package com.lelefly.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lelefly.base.Validator.LeleflyUserValidator;
import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.model.LeleflyUserVo;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.RowResult;
import com.lelefly.base.model.UpdateIdBean;
import com.lelefly.base.po.LeleflyUser;
import com.lelefly.base.serviceImp.LeleflyUserService;
import com.lelefly.base.utils.DateUtils;
import com.lelefly.base.utils.LUUIDutils;
import com.lelefly.constant.Constant;
import com.lelefly.http.BizUtil;


@Controller
@RequestMapping("/admin")
public class LeleflyUserController extends BaseController{

	@Resource
	public LeleflyUserService leleflyUserService;
	
	@Resource
	public BaseDao baseDao;
	
	@Resource
	public LeleflyUserValidator leleflyUserValidator;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/useradmin",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result userAdmin(HttpServletRequest request, HttpServletResponse response){
		Result<LeleflyUserVo> result=null;
		try {
			String phonenumber = request.getParameter("phonenumber");
			String password = request.getParameter("password");
			result = leleflyUserService.getUserReUnquie(phonenumber, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 此请求必须要校验身份证，最新的版本调用此方法
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user_login",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result userLogin(HttpServletRequest request, HttpServletResponse response){
		Result<LeleflyUserVo> result=null;
		try {
			String phonenumber = request.getParameter("phonenumber");
			String password = request.getParameter("password");
			result = leleflyUserService.getUserLoginReUnquie(phonenumber, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	@RequestMapping("/update_id_card")
	@ResponseBody
	public Result<LeleflyUser> updateIdNo(UpdateIdBean idBean){
		Result<LeleflyUser> result = new Result<>();
		if (idBean == null) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("数据为空");
			return result;
		}
		result = leleflyUserService.updateUserCardNo(idBean);
		return result;
	}
	
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/userregist",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result addUser(LeleflyUser leleflyUser,String code){
		Result<LeleflyUserVo> result=null;
		try {
			if (StringUtils.isEmpty(leleflyUser.getUsernm())) {
				leleflyUser.setUsernm(LUUIDutils.getUUID());
			}
			result = leleflyUserService.addLeleflyUser(leleflyUser,code);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		} 
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/userreset",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result resetUser(LeleflyUser leleflyUser,String code){
		Result<LeleflyUserVo> result=null;
		try {
			result = leleflyUserService.resetLeleflyUser(leleflyUser,code);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		} 
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/userupdate",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result updateUser(LeleflyUser leleflyUser){
		Result<LeleflyUserVo> result=null;
		try {
			result = leleflyUserService.updateLeleflyUser(leleflyUser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/userdel",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result delUser(String usernm){
		Result<LeleflyUser> result=null;
		try {
			result = leleflyUserService.delLeleflyUser(usernm);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/getuserinfo",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result getUserInfo(String usernm){
		Result<LeleflyUser> result=null;
		try {
			result = leleflyUserService.getUserReUnquieAndAllField(usernm);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	
	// 注册返回token,token的有效期为7天
	@RequestMapping(value="/login",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<String> login(HttpServletRequest request,
			HttpServletResponse response){
		Result<String> result = new Result<>();
		
		try {
			String phonenumber = request.getParameter("username");
			String password = request.getParameter("password");
			LeleflyUser user = leleflyUserService.getUser(phonenumber, password);
			if (user == null) {
				result.setMes("用户名或密码错误");
				result.setState(Constant.HTTP_STATUS_FAIL);
			}else {
				user.setLoginTime(DateUtils.getCurDate());
				leleflyUserService.updateLeleflyUser(user);
				String token = leleflyUserService.cacheLoginInfo(phonenumber);
				List<String> resultList = new ArrayList<>();
				resultList.add(token);
				result.setResultList(resultList);
				result.setMes("登录成功");
				result.setState(Constant.HTTP_STATUS_OK);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	@RequestMapping("/queryAllUser")
	@ResponseBody
	public RowResult<LeleflyUser> queryAllUser(HttpServletRequest request){
		
		RowResult<LeleflyUser> result = new RowResult<>();
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		result = leleflyUserService.getAllUser(start,limit);
		return result;
	}
}
